package cn.minsh.lims.eoms.modules.app.controller;

import cn.minsh.lims.eoms.common.utils.PageUtils;
import cn.minsh.lims.eoms.common.utils.R;
import cn.minsh.lims.eoms.modules.app.controller.req.InspectionPlanListReq;
import cn.minsh.lims.eoms.modules.app.controller.req.InspectionPlanReq;
import cn.minsh.lims.eoms.modules.app.controller.res.InspectionPlanRes;
import cn.minsh.lims.eoms.modules.app.entity.InspectionPlanEntity;
import cn.minsh.lims.eoms.modules.app.enums.PlanStatusEnum;
import cn.minsh.lims.eoms.modules.app.service.InspectionPlanService;
import cn.minsh.lims.eoms.common.utils.ParamUtils;
import cn.minsh.lims.eoms.modules.app.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;


/**
 * 巡检计划
 *
 * @author lht
 * @email sunlightcs@gmail.com
 * @date 2021-04-24 16:48:07
 */
@RestController
@RequestMapping("sys/inspectionPlan")
@Api(tags = "巡检计划（优化，经办人改为多个）")
public class InspectionPlanController extends AbstractController{
    @Autowired
    private InspectionPlanService inspectionPlanService;

    /**
     * 列表
     */
    @PostMapping("/list")
    @ApiOperation("巡检计划列表")
    @ApiResponses({
            @ApiResponse(code = 200,message = "ok",response =InspectionPlanRes.class )
    })
    public R list(@RequestBody InspectionPlanListReq req){
        try {
            PageUtils<InspectionPlanRes> page = inspectionPlanService.queryPage(req);
            return R.ok().put("data",page);
        }catch (Exception e){
            return R.error(e.getMessage()) ;
        }

    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("巡检计划详情")
    @ApiResponses({
            @ApiResponse(code = 200,message = "ok",response = InspectionPlanRes.class)
    })
    public R info(@PathVariable("id") Long id,
                  HttpServletRequest request,
                  HttpServletResponse response){
        try {
            InspectionPlanRes res = inspectionPlanService.queryById(id);
            return R.ok().put("data", res);
        }catch (Exception e){
           return R.error(e.getMessage()) ;
        }
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("添加巡检计划")
    public R save(@RequestBody @Valid InspectionPlanReq req,
                  BindingResult bindingResult,
                    HttpServletRequest request){
        try {
            ParamUtils.check(bindingResult);
            if (req.getFirstStartTime().after(req.getFirstEndTime())){
                return R.error("首次开始时间不能晚于结束时间") ;
            }
            if (req.getInvalidTime()!=null && req.getInvalidTime().before(req.getFirstStartTime())){
                return R.error("失效时间不能早于首次开始时间") ;
            }
            inspectionPlanService.savePlan(req,UserUtils.getUserId(request));
            return R.ok();
        }catch (Exception e){
            return R.error(e.getMessage()) ;
        }

    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation("修改巡检计划")
    public R update(@RequestBody @Valid InspectionPlanReq req,
                    BindingResult bindingResult,
                    HttpServletRequest request){
        try {
            ParamUtils.check(bindingResult);
            if (req.getId()==null){
                return R.error("计划时间不能为空") ;
            }
            if (req.getFirstStartTime().after(req.getFirstEndTime())){
                return R.error("首次开始时间不能晚于结束时间") ;
            }
            if (req.getInvalidTime()!=null && req.getInvalidTime().before(req.getFirstStartTime())){
                return R.error("失效时间不能早于首次开始时间") ;
            }
            inspectionPlanService.updatePlan(req,UserUtils.getUserId(request)) ;
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage()) ;
        }

    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除")
    public R delete(@RequestParam("id") Long id,HttpServletRequest request){
        InspectionPlanEntity planEntity = inspectionPlanService.getById(id) ;

        if (planEntity==null ||planEntity.getId()==null){
            return R.error("计划不存在") ;
        }
        if (PlanStatusEnum.NOT_STARTED.getCode().equals(planEntity.getStatus())){
            planEntity.setIsDel(1);
            planEntity.setUpdateBy(UserUtils.getUserId(request));
            planEntity.setUpdateTime(new Date());
            inspectionPlanService.updateById(planEntity) ;
        }else{
            return R.error("仅未开始状态计划可删除") ;
        }
       /* if (PlanStatusEnum.STARTED.getCode().equals(planEntity.getStatus())){
            return R.error("进行中的计划不能删除") ;
        }
        if (PlanStatusEnum.OVER.getCode().equals(planEntity.getStatus())){
            return R.error("已结束的计划不能删除") ;
        }*/
        return R.ok();
    }

}
